/******************************************************************************
 * @file led.c
 * 
 * @par dependencies
 * - bsp_led.h
 * - stdio.h
 * - stdint.h
 * 
 * @author Shawn Li <2339679615@qq.com>
 * 
 * @brief Provide the HAL APIs of LED and corresponding options.
 * 
 * @version v0.1 2025-10-15
 * 
 * @date 2025-10-15
 * 
 * @copyright Copyright (c) 2025 Li Shan
 * All rights reserved.
 * 
 * @note 1 tap == 4 spaces!
 *       Code is UTF-8
 * 
******************************************************************************/

#include "bsp_led.h"

#include "stddef.h"

//******************************** Defines **********************************//

//******************** Thread_Function ******************//
osThreadId_t led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
  .name = "led_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityHigh,
}; 
//******************** Thread_Function ******************//


//********************* Queue_Function ******************//
QueueHandle_t led_queue;
//********************* Queue_Function ******************//


//********************* Function Prototypes**************//
/**
 * @brief Instantiates the led_operation_t target.
 * 
 * Steps:
 *  1. doing the specified operations.
 *  
 * @param[in] void        : Pointer to the target of handler.
 * 
 * @return led_status_t : Status of the function.
 * 
 * */
led_status_t led_on_off(led_operation_t led_operation)
{
    led_status_t led_ret = LED_OK;
    
    if ( LED_ON     == led_operation )
    {
        // 1. Make the LED ON.
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    }
    if ( LED_OFF    == led_operation )
    {
        // 2. Make the LED OFF.
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    }
    if ( LED_TOGGLE == led_operation )
    {
        // 3. Toggle the LED.
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }
    if ( LED_BLINK  == led_operation )
    {
        // 4. Blink the LED.
        for ( uint8_t i = 0; i < ( uint8_t ) 6; i++ )
        {
            HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
            vTaskDelay(200);
        }
    }

    return led_ret;
}

/**
 * @brief led handling thread entry function.
 *
 * This FreeRTOS task polls for led events or waits for notifications and
 * dispatches led events (for example, pushing events to @p led_queue or
 * invoking a registered callback). The @p argument parameter is the value
 * passed when the task was created and may be NULL.
 *
 * @param[in] argument Pointer passed to the task (task parameter).
 * 
 * @return void
 */
void led_task_func(void *argument)
{
    led_status_t    led_ret         =          LED_OK;
    led_operation_t led_value       =          LED_ON;
    led_queue = xQueueCreate( 10, sizeof( led_operation_t ) );
    if ( NULL == led_queue )
    {
        printf("led Queue Create Failed\r\n");
    }
    else
    {
        printf("led Queue Create Success\r\n");
    }
    
    for(;;)
    {
        printf("Hello led Thread\r\n");
        
        if ( 0 != led_queue )
        {
            if ( pdTRUE == xQueueReceive(                       led_queue, 
                                                           &( led_value ), 
                                            ( TickType_t ) portMAX_DELAY))
            {
                printf("led Value Received: [%d] at [%d] tick\r\n", 
                                                         led_value, 
                                                    HAL_GetTick());
                led_ret = led_on_off( led_value );
                if ( LED_OK == led_ret )
                {
                    printf("led_on_off successfully at [%d] tick\r\n",
                                                       HAL_GetTick());
                }
            }
        }
        
        osDelay(1000);
    }
}
//********************* Function Prototypes**************//


//******************************** Defines **********************************//
